export const calcDate = (date1, date2) => {
  var date3 = date2 - date1;

  var days = Math.floor(date3 / (24 * 3600 * 1000));

  var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000));

  var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000));

  var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  var seconds = Math.round(date3 / 1000);
  return {
    leave1,
    leave2,
    leave3,
    days: days,
    hours: hours,
    minutes: minutes,
    seconds: seconds,
  };
};
/**
 * 日期格式化
 */
export function dateFormat(date) {
  let format = "yyyy-MM-dd hh:mm:ss";
  if (date != "Invalid Date") {
    var o = {
      "M+": date.getMonth() + 1, //month
      "d+": date.getDate(), //day
      "h+": date.getHours(), //hour
      "m+": date.getMinutes(), //minute
      "s+": date.getSeconds(), //second
      "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
      S: date.getMilliseconds(), //millisecond
    };
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
  }
  return "";
}

/**
 * 获取距离今天指定天数的日期
 */
export function getDate(i) {
  let date = new Date();
  date.setDate(date.getDate() + i);
  return dateFormat(date).substring(0, 10);
}

/**

* 秒转为 天小时分秒

*/

export function formatSeconds(second_time) {
  //console.log(second_time)

  if (isNaN(second_time) || second_time == "") {
    return "";
  }

  var timeStr = parseInt(second_time) + "秒";

  if (parseInt(second_time) > 60) {
    var second = parseInt(second_time) % 60;

    var min = parseInt(second_time / 60);

    timeStr = formatSecondsAppend(min, "分") + formatSecondsAppend(second, "秒");

    if (min > 60) {
      min = parseInt(second_time / 60) % 60;

      var hour = parseInt(parseInt(second_time / 60) / 60);

      timeStr = formatSecondsAppend(hour, "小时") + formatSecondsAppend(min, "分") + formatSecondsAppend(second, "秒");

      if (hour > 24) {
        hour = parseInt(parseInt(second_time / 60) / 60) % 24;

        var day = parseInt(parseInt(parseInt(second_time / 60) / 60) / 24);

        timeStr = formatSecondsAppend(day, "天") + formatSecondsAppend(hour, "小时") + formatSecondsAppend(min, "分") + formatSecondsAppend(second, "秒");
      }
    }
  }

  return timeStr;
}

/**

* 连接处理，v=0项的不要

*/

export function formatSecondsAppend(v, t) {
  return v === 0 ? "" : v + t;
}

/**
 * 在指定日期上增加天数
 */
export function strDateAdd(strDate, i) {
  let date = Date.parse(strDate);
  date.setDate(date.getDate() + i);
  return dateFormat(date).substring(0, 10);
}

/**
 *获取日期区间的每一个日期，返回日期数组
 *@method getEveryDayDateByBetweenDate
 *@start_date {date} 开始日期
 *@end_date   {date} 结束日期
 *@return {arr} 返回日期数组
 */
export function getEveryDayDateByBetweenDate(start_date, end_date) {
  var dateList = [start_date];
  if (start_date == end_date) return dateList;
  var i = 1;
  while (true) {
    var dateTime = new Date(dateList[i - 1]);
    dateTime.setDate(dateTime.getDate() + 1);
    var date = dateFormat(dateTime).substring(0, 10);
    dateList.push(date);
    if (date == end_date) {
      break;
    }
    i++;
  }
  return dateList;
}

//时间戳转换为时间格式
export function timestampDate(time, type) {
  var date = new Date(time);
  var YY = date.getFullYear();
  var MM = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  // return YY + MM + DD + " " + hh + mm + ss;
  var arr = [
    YY + "-" + MM + "-" + DD, //0
    YY + "-" + MM + "-" + DD + " " + hh + ":" + mm + ":" + ss, //1
    YY + "-" + MM + "-" + DD + " " + hh + ":" + mm, //2
    YY + "年" + MM + "月" + DD, //3
    YY + "年" + MM + "月" + DD + " " + hh + ":" + mm + ":" + ss, //4
    hh + ":" + mm + ":" + ss, //5
    MM + "-" + DD, //6
  ];
  return arr[type];
}

//格式化时间格式
export function formatDate(date, type) {
  var YY = date.getFullYear();
  var MM = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  var hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  var mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  var ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  // return YY + MM + DD + " " + hh + mm + ss;
  var arr = [
    YY + "-" + MM + "-" + DD, //0
    YY + "-" + MM + "-" + DD + " " + hh + ":" + mm + ":" + ss, //1
    YY + "-" + MM + "-" + DD + " " + hh + ":" + mm, //2
    YY + "年" + MM + "月" + DD, //3
    YY + "年" + MM + "月" + DD + " " + hh + ":" + mm + ":" + ss, //4
    hh + ":" + mm + ":" + ss, //5
    MM + "-" + DD, //6
    YY + "/" + MM + "/" + DD, //7
    YY + "" + MM + "" + DD, //8
    hh + ":" + mm + ":" + ss, //9
  ];
  return arr[type];
}

//获取指定日期：type:年月周日：前一天，上一个月等
export function getTheDay(type, sum, symbol) {
  //创建date变量
  var nowDate = new Date();
  if (type == 2) {
    nowDate.setDate(nowDate.getDate() + sum);
  } else if (type == 3) {
    nowDate.setDate(nowDate.getDate() + 7 * sum);
  } else if (type == 1) {
    nowDate.setMonth(nowDate.getMonth() + sum);
  } else if (type == 0) {
    nowDate.setYear(nowDate.getFullYear() + sum);
  }
  return formatDate(nowDate, symbol);
}

//输出当天日期格式：年*月*日
export function getDay(str) {
  var y = new Date().getFullYear(); //获取年份
  var m = new Date().getMonth() + 1; //获取月份
  var d = new Date().getDate(); //获取日
  m = m < 10 ? "0" + m : m; //月数补 0
  d = d < 10 ? "0" + d : d; //日数补 0
  return [y, m, d].join(str);
}

//获取当月的第一天
export function getFirstDay(str) {
  var y = new Date().getFullYear(); //获取年份
  var m = new Date().getMonth() + 1; //获取月份
  var d = "01";
  m = m < 10 ? "0" + m : m; //月份补 0
  return [y, m, d].join(str);
}

//获取当月的最后一天
export function getLastDay(str) {
  var y = new Date().getFullYear(); //获取年份
  var m = new Date().getMonth() + 1; //获取月份
  var d = new Date(y, m, 0).getDate(); //获取当月最后一日
  m = m < 10 ? "0" + m : m; //月份补 0
  d = d < 10 ? "0" + d : d; //日数补 0
  return [y, m, d].join(str);
}

//比较时间大小0 正常 1结束时间不能大于开始时间 2超过指定天数
export function compareDate(startTime, endTime, days) {
  // 1.转为时间戳
  var date1 = Date.parse(startTime.replace(/-/g, "/"));
  var date2 = Date.parse(endTime.replace(/-/g, "/"));
  if (date1 > date2) {
    return 1; //时间不能小于当前时间
  } else {
    var datadiff = date2 - date1;
    var time = days * 24 * 60 * 60 * 1000;
    if (datadiff > time) {
      return 2; //超过指定时间
    } else {
      return 0;
    }
  }
}

//获取开始时间到结束时间的中间日期
export function getAllDateCN(startTime, endTime) {
  var date_all = [];
  var i = 0;
  var change = new Date(startTime);
  while (endTime.getTime() - change.getTime() >= 0) {
    var y = change.getFullYear();
    var m = change.getMonth() + 1;
    var d = change.getDate();
    m = m < 10 ? "0" + m : m; //月份补 0
    d = d < 10 ? "0" + d : d; //日数补 0
    date_all[i] = y + m + d;
    change.setDate(change.getDate() + 1);
    i += 1;
  }
  return date_all;
}
